/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.redeemcode.service;

import java.util.List;
import java.util.Random;

import com.jeeplus.common.utils.IdGen;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.redeemcode.entity.RedeemCode;
import com.jeeplus.modules.redeemcode.mapper.RedeemCodeMapper;

/**
 * 兑换码Service
 * @author zhych
 * @version 2021-08-23
 */
@Service
@Transactional(readOnly = true)
public class RedeemCodeService extends CrudService<RedeemCodeMapper, RedeemCode> {

	public RedeemCode get(String id) {
		return super.get(id);
	}
	
	public List<RedeemCode> findList(RedeemCode redeemCode) {
		return super.findList(redeemCode);
	}
	
	public Page<RedeemCode> findPage(Page<RedeemCode> page, RedeemCode redeemCode) {
		return super.findPage(page, redeemCode);
	}
	
	@Transactional(readOnly = false)
	public void save(RedeemCode redeemCode) {
		super.save(redeemCode);
	}
	
	@Transactional(readOnly = false)
	public void delete(RedeemCode redeemCode) {
		super.delete(redeemCode);
	}

	/***
	 * 批量创建兑换码
	 * @param redeemCode 兑换码
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void addLoopCode(RedeemCode redeemCode) {
		for (int i = 0; i < redeemCode.getCount(); i++) {
			RedeemCode code = new RedeemCode();
			code.setCode(IdGen.uuid().substring(0, 8));
			code.setTopic(redeemCode.getTopic());
			code.setQuantity(redeemCode.getQuantity());
			code.setTicketTypeId(redeemCode.getTicketTypeId());
			code.setConvertDate(redeemCode.getConvertDate());
			code.setRemark(redeemCode.getRemark());
			code.setRedeemState("0");
			super.save(code);
		}
	}
}